namespace Greedy;

public class LC0121
{
    public int MaxProfit(int[] prices)
    {
        if (prices.Length < 2) return 0;
        
        int minPrice = prices[0];
        int maxProfit = 0;
        for (int i = 1; i < prices.Length; i++)
        {
            if (prices[i] < minPrice)
            {
                minPrice = prices[i];
            }
            else if (prices[i] > minPrice)
            {
                maxProfit = Math.Max(maxProfit, prices[i] - minPrice);
            }
        }

        return maxProfit;
    }
}